package com.diecolor.power.controller;

import com.diecolor.power.dao.RoleMapper;
import com.diecolor.power.domain.GridBean;
import com.diecolor.power.domain.ResultBean;
import com.diecolor.power.domain.Role;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("admin/role")
public class RoleController {
    @Autowired
    private RoleMapper mapper;

    @RequestMapping(value = "test",method = RequestMethod.GET)
    public String test()
    {
        return "test";
    }


    @RequestMapping(value = "list",method = RequestMethod.GET)
    public String list(HttpServletRequest request)
    {
           return "admin/role/list";
    }

    @PostMapping("list")
    @ResponseBody
    public Object getListData(Integer pageSize,Integer pageIndex)
    {
        if (pageSize==null)
        {
            pageSize=20;
        }
        if (pageIndex==null)
        {
            pageIndex=1;
        }
        PageHelper.startPage(pageIndex,pageSize);

        List<Role> roleList=mapper.selectAll(new HashMap());

        PageInfo<Role> info=new PageInfo<>(roleList);
        GridBean<Role> bean=new GridBean<>(info.getList(),info.getTotal());
        return  bean;
    }


    @PostMapping("update")
    @ResponseBody
    public Object update(Role role)
    {
        ResultBean bean=new ResultBean();
        try {

            int rows=mapper.updateByPrimaryKeySelective(role);
            if (rows>=1)
            {

            }
            else{
                bean.setCode(10001);
                bean.setMsg("修改失败");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            bean.setCode(10002);
            bean.setMsg("修改异常");
        }
        return  bean;
    }
}
